package com.ysyc.driver_ee.api;


import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

public class RetrofitClient {
    /**
     * 生产服务器
     ***/
//    public static final String BASE_URL = "http://192.168.10.127:9080/openapi/api/rest/router/";

    /**
     * 测试服务器
     ***/
    public static final String BASE_URL = "http://192.168.10.144:8080/dispath/";


    private static volatile RetrofitClient INSTANCE;

    private Retrofit retrofit;

    public static synchronized RetrofitClient getInstance() {
        if (INSTANCE == null) {
            synchronized (RetrofitClient.class) {
                if (INSTANCE == null) {
                    INSTANCE = new RetrofitClient();
                }
            }
        }

        return INSTANCE;
    }

    private RetrofitClient() {
        createRetrofit();
    }

    private Retrofit createRetrofit() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().
                connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS).
                        addInterceptor(new CommonInterceptor()).build();

        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(HttpConveterFactory.create())
                .build();

        return retrofit;
    }

    public HttpServices getHttpServices() {
        if (retrofit == null) {
            createRetrofit();
        }
        HttpServices httpServices = retrofit.create(HttpServices.class);
        return httpServices;
    }


}
